<?php
namespace app\index\controller;

use app\index\controller\Base;
use think\Db;
use think\Session;
class Person extends Base
{
    public function index()
    {
        $person_id=session('person_id');
        $class = Db::name('classperson')->where('person_id',$person_id)->find();
        $result = Db::name('courseclass')->alias('a')->join('course b','a.course_id=b.course_id','INNER')->where('a.class_id',$class['class_id'])->select();
//        $CID=Db::name('courseperson')->where('person_id',$person_id)->find();
//        $courseInf=Db::name('course')->where('course_id',$CID['course_id'])->select();
        //dump($courseInf);
        $this->assign('result',$result);
        return $this->fetch('person/index');
    }
    public function courseList()
    {
        $data = input('get.');
        if (session('status')==1){
            $project = Db::name('courseproject')->alias('a')->join('project b','a.project_id=b.project_id','RIGHT')->where('a.course_id',$data['course_id'])->select();
        }else{
            $project = Db::name('courseproject')->alias('a')->join('project b','a.project_id=b.project_id','RIGHT')->where(['a.course_id'=>$data['course_id'],'b.status'=>0])->select();
        }
        $this->assign('data',$data);
        $this->assign('project',$project);
        return $this->fetch('person/courseList');
    }
    public function courseLook()
    {
        $data = input('get.');
        if ($data['status']==1&&session('status')==0){
            $this->error('您无权进入');
        }
        $pid = session('person_id');
        $find = Db::name('grade')->where(['project_id'=>$data['project_id'],'person_id'=>$pid])->find();
        if($find){
            $this->redirect('/index/Person/makeGrade',array('project_id'=>$data['project_id']));
        }
        $this->assign('pid',$pid);
        $this->assign('data',$data);
        return $this->fetch('person/courseLook');
    }
    public function makeGrade()
    {
        $person_id = session('person_id');
        @$data = input('project_id');
        $randArr = Db::query('SELECT * FROM think_grade WHERE grade_id >= (SELECT FLOOR( MAX(grade_id) * RAND()) FROM think_grade ) and grade <1 and project_id='. $data .' and person_id !='.$person_id .' ORDER BY grade_id LIMIT 1; ');

        if ($randArr==null){
            $this->error('当前无法评分请稍后再试或评分次数已满');
        }
        $grade_id = $randArr[0]['grade_id'];
        $this->assign('grade_id',$grade_id);
        $file = Db::name('file')->where(['belong_who_id'=>$randArr[0]['person_id'],'belong_project_id'=>$randArr[0]['project_id']])->find();
        $this->assign('file',$file);
        return $this->fetch('person/makeGrade');
//        array(1) {
//        [0] => array(5) {
//            ["grade_id"] => int(3)
//            ["person_id"] => int(6)
//            ["project_id"] => int(1)
//            ["grade"] => int(0)
//            ["make_person_id"] => int(0)
//  }
//}
//array(5) {
//        ["file_id"] => int(14)
//        ["file_route"] => string(61) "/var/www/html/thinkphp/public/uploads/20191208/sunyan1236.zip"
//        ["belong_who_id"] => int(6)
//        ["belong_project_id"] => int(1)
//        ["file_name"] => string(14) "sunyan1236.zip"
//}
    }
    public function personMakeGrade()
    {
        $data = input('post.');
        $res = Db::name('grade')->where(['make_person_id'=>session('person_id'),'project_id'=>$data['project_id']])->select();
        $num = count($res);
        if ($num>=3){
            $this->error('次数达到上限');
        }
        $result = Db::name('grade')->data(['grade'=>$data['grade'],'make_person_id'=>session('person_id')])->where('grade_id',$data['grade_id'])->update();
        if ($result){
            $this->success('评分成功');
        }
        else{
            $this->assign('评分失败');
        }
    }
    public function CourseProject()
    {
        $Cid = input('get.');
       // $projectInf=Db::name('courseproject')->where('course_id',$Cid)->select();
        //dump($courseInf);
        dump($Cid);
        //$this->assign('projectInf',$projectInf);
        return $this->fetch();
    }
    public function file()
    {
        $Cid = input('get.cid');
        $projectid=Db::name('courseproject')->where('course_id',$Cid)->select();//选出课程的所有实验
       // $projectInf=Db::name('courseproject')->where('course_id',$Cid)->select();
        $data=array();
        //$this->assign('projectInf',$projectInf);
        $i=0;
        foreach ($projectid as $p):
             $projectInf=Db::name('project')->where('project_id',$p['project_id'])->find();
            $data[$i]['id']=$projectInf['project_id'];
            $data[$i]['name']=$projectInf['name'];
            $data[$i]['describe']=$projectInf['describe'];
            $data[$i]['status']=$projectInf['status'];
            ++$i;
        endforeach;
        //dump($data);
        $this->assign('projectInf',$data);
        return $this->fetch('person/file');
    }
    public function Atproject(){
        $Cid = input('get.pid');
        $this->assign('pid',$Cid);
        return $this->fetch();
    }

    public function upload(){
        $pid = input('post.pid');
        $project_id = input('post.project_id');

        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        //dump($file);

       //dump($oldname);
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $oldname= $_FILES["file"]["name"];
            //$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
//            if($file->getExtension())
            echo $file->getExtension();
            $person_id=session('person_id');
            $person_name=session('name');
            $fileName=$person_name.$pid.$project_id;
            $info = $file->validate(['ext'=>'rar,zip,tar'])->move("/var/www/html/thinkphp/public/uploads",$fileName,false);//	对文件使用md5_file散列生成文件名
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
//                echo $info->getExtension();
//                echo "<br>";
//                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
//                echo $info->getSaveName();//获取保存的文件名
//                echo "<br>";
//                // 输出 42a79759f284b767dfcb2a0197904287.jpg
//                echo $info->getFilename();
//                echo "666";
                $saveName=$info->getSaveName();
                $data=array();
                //$time=date("Ymd");
                $data['file_route']="/var/www/html/thinkphp/public/uploads"."/".$fileName.".".$info->getExtension();
                $data['file_name']=$oldname;
                $data['belong_who_id']=$person_id;
                $data['belong_project_id']=$project_id;
                $ret = Db::name('file')->insert($data);
                //echo "<script>alert('{$saveName}')</script>";
                /**
                 *
                 */
                echo "<script>alert('上传成功')</script>";
                $res1 = Db::name('grade')->data(['person_id'=>$pid,'project_id'=>$project_id])->insert();
                $res2 = Db::name('grade')->data(['person_id'=>$pid,'project_id'=>$project_id])->insert();
                $res3 = Db::name('grade')->data(['person_id'=>$pid,'project_id'=>$project_id])->insert();
                if (!$res1||!$res2||!$res3){
                    $this->error('错误');
                }
                /**
                 *
                 */
                echo "<script>alert('上传成功')</script>";
                $this->redirect('/index/Person/makeGrade',array('project_id'=>$project_id));
            }else{
                // 上传失败获取错误信息
                echo "<script>alert('{$file->getError()}')</script>";

            }
        }else{
            echo "<script>alert('上传失败,您最多可以上传25M的文件！')</script>";
        }
    }

    public function todownload()
    {
       // $time=date("Ymd");
        $filename = input('post.filename');
        $wid=request()->post('wid');
        $pid=request()->post('pid');
        $res=$CID=Db::name('file')->where(['belong_who_id'=>$wid,'belong_project_id'=>$pid])->find();
        if($res){
            $file_dir=$res['file_route'];
        }else{
            $this->error('文件未找到!');
        }
        // $filename="54f652f41b9462870dcb28d3942075da.jpeg";
       // $famlePath = $filename.$pid;//路径
        //$famlePath="20191203/54f652f41b9462870dcb28d3942075da.jpeg";

        //$file_dir = ROOT_PATH . 'public' . DS . 'uploads' . '/' . "$famlePath";    // 下载文件存放目录

        //echo "<script>alert('{$famlePath}')</script>";
        // 检查文件是否存在
        if (! file_exists($file_dir) ) {
            $this->error('文件未找到!!');
        }else{
            // 打开文件
            $file1 = fopen($file_dir, "r");
            // 输入文件标签
            $getFileName=explode('/',$file_dir,0);
            //dump($famlePath);
            //exit();
            Header("Content-type: application/octet-stream");
            Header("Accept-Ranges: bytes");
            Header("Accept-Length:".filesize($file_dir));
           if(session('person_id')){
           Header("Content-Disposition: attachment;filename=" . "asd.zip");
           }else{
            Header("Content-Disposition: attachment;filename=" . $filename);
           }
            ob_clean();     // 重点！！！
            flush();        // 重点！！！！可以清除文件中多余的路径名以及解决乱码的问题：
            //输出文件内容
            //读取文件内容并直接输出到浏览器
            echo fread($file1, filesize($file_dir));
            fclose($file1);
            exit();
        }
    }
    public function group()
    {
        $res = Db::name('group')->alias('a')->join('person b','a.person_id=b.person_id','RIGHT')->where('person_id_lead',session('person_id'))->select();
        $this->assign('res',$res);
        return $this->fetch('person/group');
    }
    public function addMember(){
        $pName=input('post.personName');
        $res=$CID=Db::name('person')->where('name',$pName)->find();
        $have=$CID=Db::name('group')->where('person_id',$res['person_id'])->find();
        if($have){
            echo "<script>alert('此人已经加入了一个组')</script>";
            exit();
        }
        $data=array();
        if($res){
            $data['person_id_lead']=session('person_id');
            $data['person_id'] =$res['person_id'];
            $ret = Db::name('group')->insert($data);
            if($ret){
                $this->success('添加成功');
            }
        }else{
            $this->error('未找到此用户');
        }
    }public function delMember(){
        $pName=input('post.personName');
        $res=$CID=Db::name('person')->where('name',$pName)->find();
        if($res){
            $who=session('person_id');
            $ret = Db::name('group')->where('person_id',$res['person_id'])->where('person_id_lead',$who)->delete();
            if($ret){
                $this->success('删除成功');
            }
        }else{
            $this->error('未找到此用户或您不是他的组长！！！！！');
            exit();
        }
    }
    public function info(){
        echo phpinfo();
    }
///////////2020/1/1  加
    public function delFile(){
        $data = input("get.");
        $project_id=$data['project_id'];
        $person_id=session("person_id");
        $isUserGrade = Db::name('gradeuser')->where(['person_id'=>$person_id,'project_id'=>$project_id])->find();

        if ($isUserGrade>0){
            $this->error('教师以评分，无法删除！');
        }else{
            $isFile = Db::name('file')->where(['belong_who_id'=>$person_id,'belong_project_id'=>$project_id])->find();
            if ($isFile>0){
                $delGrade = Db::name('grade')->where(['person_id'=>$person_id,'project_id'=>$project_id])->delete();
                if ($delGrade==0){
                    $this->error('评分删除失败');
                    exit;
                }else{
                    $fileRoute=$isFile['file_route'];
                    $fileName=$isFile['file_name'];
                    fopen($fileRoute,'a+');
                    if(!unlink($fileRoute))
                    {
                        echo "<script>alert(\"文件{$fileName}删除失败\")</script>";
                    }
                    else
                    {
                        echo "<script>alert(\"文件{$fileName}删除成功\")</script>";
                    }
                    $delFile = Db::name('file')->where(['belong_who_id'=>$person_id,'belong_project_id'=>$project_id])->delete();
                    if ($delFile>0){
                        $this->success('文件路径删除成功！');
                    }else{
                        $this->success('文件路径删除失败！');
                    }
                }
            }else{
                $this->error('文件不存在！');
                exit;
            }

        }
    }
}
